﻿using System;
using System.ComponentModel.DataAnnotations;
using DataAccess.Entities;
using Zeenck.DataAccess;

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
public class ValidateEmailRegisteredAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        Boolean isValid;

        try
        {
            if (!value.GetType().Equals(typeof(string)))
            {
                isValid = false;
            }
            else
            {
                Account account = AccountModule.ReadProfile(value as String);

                if (account != default(Account))
                {
                    isValid = true;
                }
                else
                {
                    isValid = false;
                }
            }
        }
        catch (Exception)
        {
            isValid = false;
        }

        return isValid;
    }
}
